/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.agent.behaviour;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.openide.awt.StatusDisplayer;
import pl.tom.social.analyzer.jade.LocalJade;
import pl.tom.social.common.types.ConversationIDType;

/**
 *
 * @author Tom
 */
public class GlobalBehaviour extends CyclicBehaviour {

	private static final Logger logger = Logger.getLogger(GlobalBehaviour.class);

	@Override
	public void action() {
		ACLMessage msg = myAgent.blockingReceive();
		if (msg != null) {
			String cId = msg.getConversationId();
			if (cId != null && cId.length() > 0) {
				try {
					ConversationIDType type = ConversationIDType.valueOf(cId);
					switch (type) {
						case GetTaskBar:
							LocalJade.getInstance().addTaskBarAgent(msg.getSender());
							break;
						case Notify:
							String notifyMsg;
							if (msg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) {
								notifyMsg = "Task accepted by " + msg.getSender().getName();
								logger.log(Level.DEBUG, notifyMsg);
							} else {
								notifyMsg = "Task rejected by " + msg.getSender().getName();
								logger.log(Level.DEBUG, notifyMsg);
							}
							StatusDisplayer.getDefault().setStatusText(notifyMsg);
							break;
					}
				} catch (IllegalArgumentException ex) {
					logger.log(Level.ERROR, null, ex);
				}
			}
		}
	}
}
